package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.promotion.entity.dos.ReturnActivity;
import cn.lili.modules.promotion.entity.enums.ReturnActivityStatusEnum;
import cn.lili.modules.promotion.mapper.ReturnActivityMapper;
import cn.lili.modules.promotion.service.ReturnActivityService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ReturnActivityServiceImpl extends ServiceImpl<ReturnActivityMapper, ReturnActivity> implements ReturnActivityService {

    /**
     * 获取全返卷面额列表
     *
     * @param page       分页条件
     * @param queryParam 查询条件
     */
    @Override
    public Page<ReturnActivity> getPage(ReturnActivity queryParam, PageVO page) {
        //构建查询条件
        LambdaQueryWrapper<ReturnActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(StringUtils.isNotEmpty(queryParam.getName()),ReturnActivity::getName,queryParam.getName());
        queryWrapper.eq(queryParam.getPrice() != null,ReturnActivity::getPrice,queryParam.getPrice());
        queryWrapper.eq(StringUtils.isNotEmpty(queryParam.getStatus()),ReturnActivity::getStatus,queryParam.getStatus());
        queryWrapper.orderByDesc(ReturnActivity::getCreateTime);
        //查询
        return this.baseMapper.selectPage(PageUtil.initPage(page), queryWrapper);
    }

    /**
     * 发布全返卷面额
     * @param id 全返卷面额ID
     * @return 成功/失败
     * */
    @Override
    public Boolean already(String id) {
        ReturnActivity updateParam = new ReturnActivity();
        updateParam.setId(id);
        updateParam.setStatus(ReturnActivityStatusEnum.PUBLISH_ALREADY.getCode());
        return this.updateById(updateParam);
    }

    /**
     * 查询全返卷面额 数量大于0 状态为已发布 删除标识为false
     *
     * @return 可用全返卷面额集合
     * */
    @Override
    public List<ReturnActivity> getReturnActivity(){
        LambdaQueryWrapper<ReturnActivity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ReturnActivity::getDeleteFlag,false);
        lambdaQueryWrapper.eq(ReturnActivity::getStatus, ReturnActivityStatusEnum.PUBLISH_ALREADY.getCode());
        lambdaQueryWrapper.gt(ReturnActivity::getNumber,0);
        List<ReturnActivity> list = this.list(lambdaQueryWrapper);
        if(list == null || list.isEmpty()){
            throw new ServiceException("可用全返卷面额不足");
        }
        return list;
    }
}
